// pages/TradingAreaCenter/activityCenter.js
const controller=require("../controllers/Controller").controller


const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: app.globalData.tabbar,
    tabPage: app.globalData.tabPage,
    title:"",
    stateList:[{
      name:"全部状态",
      id:''
    },{
      name:"正常",
      id:0
    },{
      name:"冻结",
      id:1
    }],
    timeStateList:[{
      name:"全部时刻",
      id:''
    },{
      name:"未开始",
      id:0
    },{
      name:"进行中",
      id:1
    },{
      name:"结束",
      id:2
    }],
    index1:0,
    index2:0,
    index3:0,
    type:'',
    state:'',
    timeState:'',
    pageNumber:1,
    lastPage:false,
    list:[],
    shopCircleList:[],
    // 
    tablist:['全部','进行中','已结束'],
    currentIndex:0,
    search:"",
    timer:'',
  },
  tabChange(e) {
    app.tabChange(e);
  },


  bindShopCircle(e){
    let id=this.data.shopCircleList[parseInt(e.detail.value)].id
    console.log(id)
    wx.navigateTo({
      url: '/pages/TradingAreaCenter/setTopPictures?id='+id,
    })
  },

  //获取商圈列表
  myShopGroupList(){
    controller.myShopGroupList().then(res=>{
      this.setData({
        shopCircleList:res.data
      })
    })
  },
 //冻结解冻优惠券
 closeCoupon(e) {
  let value = {}
  let state = e.detail.state
  let id = e.detail.id

  if (state === 0) {
    state = 1
  } else {
    state = 0
  }
  value.couponBagId = id
  value.state = state
  controller.openOrcloseCoupon(value).then(res => {
    wx.showToast({
      icon: "none",
      title: '操作成功',
      duration: 1500
    })
    this.getBagPage()
    this.myShopGroupList()
  })
},

  

  addActivity(){
    wx.navigateTo({
      url: '/pages/TradingAreaCenter/addCouponBag?type=0',
    })
  },
  setTopPictures(){
    wx.navigateTo({
      url: '/pages/TradingAreaCenter/setTopPictures',
    })
  },
  examineList(){
    let type=this.data.type
    wx.navigateTo({
      url: '/pages/TradingAreaCenter/examineList?type='+type,
    })
  },
  

  handleInput(e){
    let search=e.detail.value;
    this.setData({
      search
    })
    if(this.timer)clearTimeout(this.timer)
    this.timer = setTimeout(() => {
      this.getBagPage()
    }, 500);
  },


  bindchangeType(e){
    let index=e.detail.value;
    this.setData({
      index1:index,
      type:this.data.typeList[index].id
    })
    this.getBagPage()
  },
  // bindchangeState(e){
  //   let index=e.detail.value;
  //   this.setData({
  //     index2:index,
  //     state:this.data.stateList[index].id
  //   })
  //   this.getBagPage()
  // },
  // bindchangeTime(e){
  //   let index=e.detail.value;
  //   this.setData({
  //     index3:index,
  //     timeState:this.data.timeStateList[index].id
  //   })
  //   this.getBagPage()
  // },

  changeTabList(e){
    let index=e.currentTarget.dataset.index;
    let timeState=''
    if(index == 1 || index == 2)timeState=index
    this.setData({
      currentIndex:index,
      timeState
    }) 
    this.getBagPage()
  },
  //代理商管理券礼包列表 2.0
  getBagPage(){
    let value={}
    value.pageNumber=1
    value.state=this.data.state
    value.timeState=this.data.timeState
    value.type=this.data.type
    value.name=this.data.search
    controller.getBagPage(value).then(res=>{
      let list = res.data
      list.forEach(item=>{
        switch(item.timeState){
          case 0:
            item.stateName = "未开始";
            item.stateColor = "grey";
            break;
          case 1:
            item.stateName = "进行中";
            item.stateColor = "green";
            break;
          case 2:
            item.stateName = "结束";
            item.stateColor = "red";
            break;
        }
      })
      
      this.setData({
        list,
        // lastPage:res.data.lastPage
      })
    })
  },
  // getMore(){
  //   if(this.data.lastPage==false){
  //     let value={}
  //     value.pageNumber=this.data.pageNumber+1
  //     this.setData({
  //       pageNumber:value.pageNumber
  //     })
  //     value.state=this.data.state
  //     value.timeState=this.data.timeState
  //     value.type=this.data.type
  //     controller.getBagPage(value).then(res=>{
  //       let list=this.data.list
  //       let list2=res.data.list
  //       let list3=list.concat(list2)
  //       this.setData({
  //         list:list3,
  //         lastPage:res.data.lastPage
  //       })
  //     })
  //   }
  // },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type
    })
    if(this.data.type==2){
      this.setData({
        title:"店铺爆款列表"
      })
    }else if(this.data.type==3){
      this.setData({
        title:"欢乐拼列表"
      })
    }else if(this.data.type==4){
      this.setData({
        title:"限时抵扣列表"
      })
    }else if(this.data.type==5){
      this.setData({
        title:"店铺超值礼包列表"
      })
    }else{
      this.setData({
        title:"一卡通列表"
      })
    }
    this.getBagPage()
    this.myShopGroupList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})